package com.experienceBaby.Cashbook.utils;

import android.annotation.SuppressLint;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    static String strDateFormat = "yyyy-MM-dd";
    @SuppressLint("SimpleDateFormat")
    static SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
    /**
     * 一天的毫秒数
     */
    private static final int DAY_OF_MILLISECOND = 86400000;

    /**
     * @param time 当前的时间戳
     * @return 0:今天开始的时间戳   1:结束的时间戳
     */
    public static Long[] getToday(Long time) {
        Long t[] = new Long[2];
        Date date = getDate(time);
        t[0] = date.getTime();
        t[1] = DAY_OF_MILLISECOND + t[0] - 1;
        return t;
    }

    /**
     * @param time 当前时间
     * @return 0：周一  1：周日的最后毫秒数
     */
    public static Long[] getThisWeek(Long time) {
        Long t[] = new Long[2];
        Date date = getDate(System.currentTimeMillis());
        t[0] = date.getTime() - (long) (getWeekToday(date.getTime()) - 1) * DAY_OF_MILLISECOND;
        t[1] = date.getTime() + (long) (8 - getWeekToday(date.getTime())) * DAY_OF_MILLISECOND - 1;
        return t;
    }

    /**
     * @param time
     * @return
     */
    public static Long[] getThisMonth(Long time) {
        Long t[] = new Long[2];
        Date date = getDate(time);
        t[0] = date.getTime() - ((long) (date.getDate() - 1) * DAY_OF_MILLISECOND);
        t[1] = date.getTime() + ((long) (getDayOfMonth(time) - date.getDate() + 1) * DAY_OF_MILLISECOND) - 1;
        return t;
    }

    public static Long[] getThisYear(Long time) {
        Long t[] = new Long[2];
        Date date = getDate(time);
        date.setDate(1);
        date.setMonth(0);
        t[0] = date.getTime();
        date.setMonth(12);
        date.setDate(1);
        t[1] = date.getTime() - 1;
        return t;
    }

    public static int getDayOfMonth(long time) {
        Date date = new Date(time);
        System.out.println(date.getMonth());
        switch (date.getMonth() + 1) {
            case 2:
                return isLeapYear(date.getYear()) ? 29 : 28;
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            default:
                return 30;
        }
    }

    public static boolean isLeapYear(int year) {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

    /**
     * @param time 当前时间
     * @return 今天是周几，周一为1，周日为7
     */
    private static int getWeekToday(Long time) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(time));
        int week = c.get(Calendar.DAY_OF_WEEK);
        if (week == 1) {
            return 7;
        } else {
            return week - 1;
        }
    }

    /**
     * @param year  年
     * @param month 月
     * @param day   日
     * @return 今天是周几，周一为1，周日为7
     */
    public static int getWeekToday(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(getDate(year, month, day));
        int week = c.get(Calendar.DAY_OF_WEEK);
        if (week == 1) {
            return 7;
        } else {
            return week - 1;
        }
    }

    /**
     * 如果不用new Date(0)的话，毫秒会有差别。无法清零毫秒
     *
     * @param time 当前的时间
     * @return 秒、分、时默认为0
     */
    public static Date getDate(Long time) {
        Date d = new Date(time);
        Date date = new Date(0);
        //清零时区
        date.setHours(0);
        date.setYear(d.getYear());
        date.setMonth(d.getMonth());
        date.setDate(d.getDate());
        return date;
    }

    public static Date getDate(int year, int month, int day) {
        Date date = new Date(0);
        date.setDate(day);
        date.setYear(year - 1900);
        date.setMonth(month - 1);
        return date;
    }

    public static String getS_Date(Date date) {
        return sdf.format(date);
    }
}
